<?php
/*
 * Aclass_area 导航类
 * 2010-07-08
 * http://www.abans.cn		abans@qq.com
 */
define('Acache_no','1');
class Aclass_area{
	function  __construct() {
		$this->cname = 'Acan:area:';
	}
	function getjson($level=1,$since=1,$add=1){
		return Ajson_encode($this->getdata($level,$add));
	}
	function getdata($level=1,$since=1,$add=1){
		$cname = $this->cname.$level.$since.':'.$add;
		$areadata = Afun_cache_get($cname);
		if(empty($areadata)){
			$area = Afun_db()->model('acan_area');
			$sheng = $area->findAll('class='.$since);
			if($since==0){$addtitle = "所有";}else{$addtitle = "全国";}
			if($add==1){
				$areadata[$since][$since] = $addtitle;
			}
			foreach($sheng as $k=>$v){
				$areadata[$since][$v[id]] = $v[name];
			}
			if($level>1){
				for($i=$since+1;$i<$level+$since;$i++){
					$shi = $area->findAll('class='.$i);
					foreach($shi as $k=>$v){
						if($add==1){
							$areadata[$i][$shi[$k][pid]][0] = "";
						}
						$areadata[$i][$v[pid]][$v[id]] = $v[name];
					}
				}
			}
			Afun_cache_set($cname,$areadata);//写入缓存
		}
		return $areadata;
	}
	//地区解码 输入json字符串 {"1":"120","2":"1130"}或者对象 返回字符串 '浙江,宁波'
	function title($str,$since=1){
		if(is_string($str)){
			$arr = Afun_obj_arr(json_decode($str));
		}elseif(is_object($str)){
			$arr = Afun_obj_arr($str);
		}else{
			$arr = $str;
		}
		$level = count($arr);
		if($level<1){
			return false;
		}
		$area = $this->getdata($level,$since);
		$uni = '';
		foreach($arr as $k=>$v){
			if($k==$since){
				$uni .= $area[$k][$v];
			}else{
				$uni .= ','.$area[$k][$pid][$v];
			}
			$pid = $v;
		}
		return $uni;
	}
	//地区编码 输入 '浙江,宁波' or array(1=>'浙江',2=>'宁波') 返回json字符串 {"1":"120","2":"1130"}
	function encode($str){
		if(is_string($str)){
			$srr = explode(',',$str);
			for ($i=0;$i<count($srr);$i++) {
				$arr[$i+1] = $srr[$i];
			}
		}elseif(is_array($str)){
			$arr = $str;
		}else{
			return false;
		}
		$area = Afun_db()->model('acan_area');
		for ($i=1;$i<count($arr)+1;$i++) {
			if(strlen($arr[$i])>0){
				$info = $area->find("class=$i and name LIKE '".$arr[$i]."%'");
				if($info['id']){
					$data[$i] = $pid = $info['id'];
				}else{
					//--超过5个字的取前四个再次查询
					if(strlen($arr[$i])>12){
						$city = substr($arr[$i],0,12);
					}else{
						$city = $arr[$i];
					}
					$info = $area->find("class=$i and pid=$pid and name LIKE '".$city."%'");
					if($info['id']){
						$data[$i] = $info['id'];
						if($data[$i-1]!=$info['pid']){
							echo '与上级城市不匹配!';
							return false;
						}
					}else{
						//--再次查询下级城市
						$cl = $i+1;
						$info = $area->find("class=$cl and name LIKE '".$city."%'");
						if($info['id']){
							if(empty($data[$cl-1])){
								$cls = $cl-1;
								$info_s = $area->findPk($info['pid']);
								if($info_s['pid']==$data[$cl-2]){
									$data[$cls] = $info_s['id'];
								}else{
									echo '与上级城市不匹配!';
									return false;
								}
							}
							$data[$cl] = $info['id'];
						}else{
							return false;
						}
					}
				}
			}
		}
		if(count($data)>0){
			return Ajson_encode($data);
		}else{
			return false;
		}
	}
}

?>
